package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* ClipView8
* */
class ClipView8 @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            color = context.resources.getColor(R.color.color_F3236D)
        }
    }

    private val mPath0 by lazy {
        Path()
    }

    private val mPath1 by lazy {
        Path()
    }

    private val mPath2 by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            save()
            translate(cx, cy)

            mPath0.apply {
                reset()
                fillType = Path.FillType.EVEN_ODD
                addCircle(
                    0F,
                    -cy / 2,
                    cx / 2,
                    Path.Direction.CCW
                )
                addCircle(
                    -cx/ 2,
                    0F,
                    cx/ 2,
                    Path.Direction.CCW
                )
                addCircle(
                    cx/ 2,
                    0F,
                    cx/ 2,
                    Path.Direction.CCW
                )
                addCircle(
                    0F,
                    cy/ 2,
                    cx/ 2,
                    Path.Direction.CCW
                )
            }

            mPath1.apply {
                addArc(
                    -cx * 17 / 24,
                    -cy * 17 / 24,
                    cx * 17 / 24,
                    cy * 17 / 24,
                    0F,
                    360F
                )
//                addArc(
//                    -cx * 20 / 24,
//                    -cy * 20 / 24,
//                    cx * 20 / 24,
//                    cy * 20 / 24,
//                    0F,
//                    360F
//                )
            }

            mPath2.apply {
                reset()
                fillType = Path.FillType.INVERSE_WINDING
                addRect(
                    -cx,
                    -cy,
                    cx,
                    cy,
                    Path.Direction.CW
                )
                addPath(mPath0)
                clipPath(mPath1)
            }

            drawPath(mPath2, mPaint0)

            restore()
        }
    }
}